package dao.impl;

import bean.dto.AnnouncementPage;
import bean.dto.EquipmentPage;
import bean.pojo.Announcement;
import bean.pojo.ComplaintRecord;
import commons.dbHelper.DBHelper;
import commons.jdbc.basic.JDBCUtil;
import dao.AnnouncementDao;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * dao.impl
 * User: xlh
 * Date: 2024/12/23 14:26
 * motto:推到高塔
 * Description:
 * Version: V1.0
 */
public class AnnouncementDaoImpl implements AnnouncementDao {
    private static DBHelper dbHelper=new DBHelper();
    @Override
    public List<Announcement> findByPage(AnnouncementPage announcementPage) {
        List<Object> list = new ArrayList<>();
        StringBuffer sql = new StringBuffer("" +
                " select announcement_id, announcement_title,announcement_text,announcement_add_time from announcement where 1=1 ");
        if (announcementPage.getSelectMonth()!= null) {
            System.out.println(announcementPage.getSelectMonth());
            sql.append(" and DATE_FORMAT(announcement_add_time, '%Y-%m') = DATE_FORMAT(STR_TO_DATE(?, '%Y-%m'), '%Y-%m') ");
            list.add(announcementPage.getSelectMonth().trim());
        }
        if (announcementPage.getAnnouncementTitle()!= null && announcementPage.getAnnouncementTitle().trim().length() > 0) {
            sql.append("  and  announcement_title like  concat('%',?,'%')  ");
            list.add(announcementPage.getAnnouncementTitle().trim());
        }
        sql.append(" order by announcement_add_time desc ");
        sql.append(" limit " + (announcementPage.getPage() - 1) * announcementPage.getLimit() + ", " + announcementPage.getLimit() );
        // 打印 SQL 语句用于调试，可根据实际情况选择保留或删除
        System.out.println("sql:" + sql);
        List<Announcement> beanList = dbHelper.getBeanList(Announcement.class, sql.toString(), list.toArray());
        return beanList;
    }
    @Override
    public Long findByTotal(AnnouncementPage announcementPage) {
        StringBuffer   sql=new StringBuffer("select  count(*) ct  from  announcement  where  1=1 ");
        //集合参数
        List<Object>  parameters=new ArrayList<>();
        //验证是否为空  null  "    "
        if(announcementPage.getAnnouncementTitle()!=null){
            parameters.add(announcementPage.getAnnouncementTitle());
            sql.append(" and  announcement_title like  concat('%',?,'%')");
        }
        //验证是否为空  null  "    "
        if(announcementPage.getSelectMonth()!=null){
            parameters.add(announcementPage.getSelectMonth());
            sql.append(" and DATE_FORMAT(announcement_add_time, '%Y-%m') = DATE_FORMAT(STR_TO_DATE(?, '%Y-%m'), '%Y-%m') ");
        }
        System.out.println("sql:"+sql);
        System.out.println("参数:"+parameters);
        JDBCUtil jdbcUtil=new JDBCUtil();
        List<Map<String, Object>> query = jdbcUtil.query(sql.toString(), parameters.toArray());
        //默认0条
        Long  ct=0L;
        if(query.size()>0){
            //改变
            ct=(Long)query.get(0).get("ct");
        }
        return ct;
    }

    @Override
    public boolean updatAnnouncement(Announcement announcement) {
        String sql="update announcement set announcement_title=?,announcement_text=?," +
                "announcement_add_time=? where announcement_id=? ";
        announcement.setAnnouncementAddTime(LocalDateTime.now());
        return dbHelper.update(sql,announcement.getAnnouncementTitle(),announcement.getAnnouncementText(),announcement.getAnnouncementAddTime(),announcement.getAnnouncementId())>0;
    }

    @Override
    public boolean addAnnouncement(Announcement announcement) {
        String sql="insert announcement (" +
                "announcement_title,announcement_text ,announcement_add_time ) values(?,?,?)";

        announcement.setAnnouncementAddTime(LocalDateTime.now());
        return dbHelper.update(sql,announcement.getAnnouncementTitle(),announcement.getAnnouncementText(),announcement.getAnnouncementAddTime())>0;
    }

    @Override
    public boolean deleteAnnouncement(Integer announcementId) {
        String sql = "DELETE FROM announcement WHERE announcement_id =? ";
        return dbHelper.update(sql,announcementId)>0;
    }
}
